﻿using System;
using System.Web.UI;
using Basic4Net.BasicItem;
using Basic4Net.BusinessRules;
using System.Web.UI.HtmlControls;
using Basic4Net.Entities;
using System.Linq;

namespace Basic4Net.OAWeb.Base
{
    public class BasePage : Page
    {
        private static Amount _amount;
        private static Amount HAmount
        {
            get { return _amount ?? (_amount = new Amount()); }
        }

        private static string _schoolMark;
        protected static string SchoolMark
        {
            get
            {
                // TODO:获取schoolMark
                _schoolMark = "School";
                return _schoolMark;
            }
        }

        protected static int CurrentYear
        {
            get
            {
                var a = HAmount.GetActiveAmount(SchoolMark);
                if (a == null)
                    return -1;
                if (a.FYear.HasValue)
                    return HAmount.GetActiveAmount(SchoolMark).FYear.Value;
                return -1;
            }
        }

        protected static int CurrentMonth
        {
            get
            {
                var a = HAmount.GetActiveAmount(SchoolMark);
                if (a == null)
                    return -1;
                if (a.FMonth.HasValue)
                    return HAmount.GetActiveAmount(SchoolMark).FMonth.Value;
                return -1;
            }
        }

        protected override void OnPreLoad(EventArgs e)
        {
            base.OnPreLoad(e);
            if ((this.ViewState[SessionConstants.UserInfo] == null) && (this.Session[SessionConstants.UserInfo] != null))
            {
                this.ViewState[SessionConstants.UserInfo] = this.Session[SessionConstants.UserInfo];
            }
        }



        #region 扩展属性

        /// <summary>
        /// Page的默认Title
        /// </summary>
        public string WebFormTitle
        {
            get { return Constants.PAGETITLE; }
        }

        /// <summary>
        /// 当前页的相对路径
        /// </summary>
        public string Path
        {
            get
            {
                int i = 1;

                string url1 = this.Page.TemplateSourceDirectory;
                if (url1.EndsWith("/"))
                    url1 = url1.Substring(0, url1.Length - 1);

                string url2 = this.Page.AppRelativeTemplateSourceDirectory.Remove(0, 1);
                if (url2.EndsWith("/"))
                    url2 = url2.Substring(0, url2.Length - 1);

                if (url1 == url2)
                    i = 1;
                else if (url1.EndsWith(url2))
                    i = 2;

                string[] temp = url1.Split('/');
                string str = "../";
                string str2 = "";

                for (; i < temp.Length; i++)
                {
                    str2 += str;
                }
                return str2;
            }
        }

        ///// <summary>
        ///// 登陆系统的用户信息

        ///// </summary>
        //public UserInfo CurrentUser
        //{
        //    set
        //    {
        //        if (this.ViewState[SessionConstants.UserInfo] == null)
        //        {
        //            this.ViewState[SessionConstants.UserInfo] = value;
        //        }
        //        this.Session[SessionConstants.UserInfo] = value;
        //    }
        //    get
        //    {
        //        UserInfo _User = null;
        //        if ((this.ViewState[SessionConstants.UserInfo] == null) && (this.Session[SessionConstants.UserInfo] != null))
        //        {
        //            this.ViewState[SessionConstants.UserInfo] = this.Session[SessionConstants.UserInfo];
        //            _User = (UserInfo)this.ViewState[SessionConstants.UserInfo];
        //        }
        //        if ((this.ViewState[SessionConstants.UserInfo] != null) && (this.Session[SessionConstants.UserInfo] != null))
        //        {
        //            _User = (UserInfo)this.ViewState[SessionConstants.UserInfo];
        //            UserInfo __user = (UserInfo)this.Session[SessionConstants.UserInfo];

        //            if (_User.ID != __user.ID)
        //            {
        //                Response.Redirect(this.Path + Constants.ERRPAGE + "?Content=0041");
        //            }
        //            else
        //            {
        //                return __user;
        //            }
        //        }

        //        return _User;
        //    }
        //}

        ///// <summary>
        ///// 判断用户是否在线
        ///// </summary>
        //public bool IsUserOnline
        //{
        //    get
        //    {
        //        if (this.CurrentUser == null)
        //            return false;
        //        return true;
        //    }
        //}

        /// <summary>
        /// 判断Session是否超时,true未超时,false超时.
        /// </summary>
        //public bool SessionStatus
        //{
        //    get
        //    {
        //        if (this.CurrentUser == null)
        //            return false;
        //        return true;
        //    }
        //}

        /// <summary>
        /// 截断字符串，并加载...
        /// </summary>
        /// <param name="source"></param>
        /// <param name="length"></param>
        /// <returns></returns>
        public static string Substr(object source, int length)
        {
            if (source == null) return "&nbsp;";
            return source.ToString()._Substr(length);
        }

        public string NavigateName
        {
            get
            {
                if (this.ViewState["NavigateName"] == null)
                    this.ViewState["NavigateName"] = String.Empty;
                return (string)this.ViewState["NavigateName"];
            }
            set
            {
                this.ViewState["NavigateName"] = value;
            }
        }

        #endregion

        #region 可扩展方法

        public void ShowMessage()
        {
            //未实现

        }

        ///// <summary>
        ///// 判断用户是否具有该权限

        ///// </summary>
        ///// <param name="RightName"></param>
        ///// <returns></returns>
        //public bool CheckRight(string RightName)
        //{
        //    if (CurrentUser == null)
        //        return false;
        //    else
        //        return CurrentUser.CheckRight(RightName);
        //}

        /// <summary>
        /// 判断用户是否具有该权限

        /// </summary>
        /// <param name="ruleID"></param>
        /// <returns></returns>
        //public bool CheckRight(int ruleID)
        //{
        //    if (CurrentUser == null) return false;
        //    return CurrentUser.RuleList.Contains(ruleID);
        //}

        /// <summary>
        /// 跳转到主页面
        /// </summary>
        public void RedirectMain()
        {
            Response.Redirect(Page.ResolveClientUrl("~/WebApp/Home/DefaultNonRule.htm"));
        }

        #endregion

        #region 获取当前核算进度日期
        /// <summary>
        /// 获取当前核算进度日期,其中天数没有任何意义
        /// </summary>
        /// <returns>返回当前核算进度日期,其中天数没有任何意义；如果找不到任何一个核算日期则返回一个日期最大值</returns>
        public static DateTime GetProDate()
        {
            using (DBEntities db = new DBEntities())
            {
                int count = (from a in db.t_Amount
                             where a.FPlan == 1
                             select a).Count();
                if (count > 0)
                {
                    t_Amount model = (from a in db.t_Amount
                                      where a.FPlan == 1
                                      select a).FirstOrDefault();
                    return Convert.ToDateTime(model.FYear.ToString() + "-" + model.FMonth.ToString() + "-" + "1");
                }
                else
                {
                    return DateTime.MaxValue;
                }
            }
        }
        #endregion

        #region 获取当前核算进度年
        /// <summary>
        /// 获取当前核算进度年
        /// </summary>
        /// <returns>返回当前核算进度年；如果找不到任何一个核算进度年则返回-1</returns>
        public static int GetProDateYear()
        {
            using (DBEntities db = new DBEntities())
            {
                int count = (from a in db.t_Amount
                             where a.FPlan == 1
                             select a).Count();
                if (count > 0)
                {
                    t_Amount model = (from a in db.t_Amount
                                      where a.FPlan == 1
                                      select a).FirstOrDefault();
                    return (int)model.FYear;
                }
                else
                {
                    return -1;
                }
            }
        }
        #endregion

        #region 获取当前核算进度月
        /// <summary>
        /// 获取当前核算进度月
        /// </summary>
        /// <returns>返回当前核算进度月；如果找不到任何一个核算进度月则返回-1</returns>
        public static int GetProDateMonth()
        {
            using (DBEntities db = new DBEntities())
            {
                int count = (from a in db.t_Amount
                             where a.FPlan == 1
                             select a).Count();
                if (count > 0)
                {
                    t_Amount model = (from a in db.t_Amount
                                      where a.FPlan == 1
                                      select a).FirstOrDefault();
                    return (int)model.FMonth;
                }
                else
                {
                    return -1;
                }
            }
        }
        #endregion

        #region 获取当前核算进度学期
        /// <summary>
        /// 获取当前核算进度学期
        /// </summary>
        /// <returns>返回当前核算进度学期；如果找不到任何一个核算进度学期则返回空字符串</returns>
        public static string GetProDateTerm()
        {
            using (DBEntities db = new DBEntities())
            {
                int count = (from a in db.t_Amount
                             where a.FPlan == 1
                             select a).Count();
                if (count > 0)
                {
                    t_Amount model = (from a in db.t_Amount
                                      where a.FPlan == 1
                                      select a).FirstOrDefault();
                    if ((int)model.Term == 1)
                        return "上学期";
                    else
                        return "下学期";
                }
                else
                {
                    return "";
                }
            }
        }
        #endregion

        #region 获取当前核算进度文字描述
        /// <summary>
        /// 获取当前核算进度文字描述
        /// </summary>
        /// <returns>返回当前核算进度文字描述；如果找不到任何一个核算日期则返回空字符串</returns>
        public static string GetProDateStr()
        {
            using (DBEntities db = new DBEntities())
            {
                int count = (from a in db.t_Amount
                             where a.FPlan == 1
                             select a).Count();
                if (count > 0)
                {
                    t_Amount model = (from a in db.t_Amount
                                      where a.FPlan == 1
                                      select a).FirstOrDefault();
                    return "当前核算日期为"+model.FYear.ToString() + "年" + model.FMonth.ToString() + "月";
                }
                else
                {
                    return "";
                }
            }
        }
        #endregion

        #region 判断教工是否存在
        /// <summary>
        /// 判断教工是否存在
        /// </summary>
        /// <param name="TeacherID">教工ID</param>
        /// <returns>返回教工是否存在，存在为true，否则为false</returns>
        public static bool ExistTeacher(int TeacherID)
        {
            bool result = false;
            BusinessRules.TeacherInfo bll = new BusinessRules.TeacherInfo();
            if(bll.ExistTeacher(TeacherID))
            {
                result = true;
            }
            return result;
        }
        #endregion

        #region 获取教工详细信息
        /// <summary>
        /// 获取教工详细信息
        /// </summary>
        /// <param name="TeacherID">教工ID</param>
        /// <returns>返回教工信息</returns>
        public static ListTeacherInfo GetTeacher(int TeacherID)
        {
            return (new BusinessRules.TeacherInfo()).GetTeacher(TeacherID);
        }
        #endregion

        #region 判断是否有访问权限
        /// <summary>
        /// 判断是否有访问权限
        /// </summary>
        /// <param name="TeacherID">教工ID</param>
        /// <param name="Permit">权限ID</param>
        /// <returns>返回是否拥有访问权限，是为true,否为false</returns>
        [System.Web.Services.WebMethod]
        public static bool ExistPermit(int TeacherID, int Permit)
        {
            return (new BusinessRules.TeacherInfo()).ExistPermit(TeacherID, Permit);
        }
        #endregion

        #region 判断是否超级用户
        /// <summary>
        /// 判断是否超级用户
        /// </summary>
        /// <param name="TeacherID">教工ID</param>
        /// <returns>返回是否超级用户，是为true,否为false</returns>
        [System.Web.Services.WebMethod]
        public static bool ExistSuper(int TeacherID)
        {
            return (new BusinessRules.TeacherInfo()).ExistSuper(TeacherID);
        }
        #endregion

        #region 获取教工的所有权限
        /// <summary>
        /// 获取教工的所有权限
        /// </summary>
        /// <param name="TeacherID">教工ID</param>
        /// <returns>返回教工的所有权限</returns>
        public static string GetPermit(int TeacherID)
        {
            return (new BusinessRules.TeacherInfo()).GetPermit(TeacherID);
        }
        #endregion

        #region 获取教工所有职务
        /// <summary>
        /// 获取教工所有职务
        /// </summary>
        /// <param name="TeacherID">教工ID</param>
        /// <returns>返回教工所有职务</returns>
        public static string GetPost(int TeacherID)
        {
            return (new BusinessRules.TeacherInfo()).GetPost(TeacherID);
        }
        #endregion
    }
}